package com.artPreter.controller.pc;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.artPreter.utils.PageUtils.getPageList;

@Controller
public class Search {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IDepartTypeService departTypeService;
    @Autowired
    private ISystemConfigService systemConfigService;

    @RequestMapping("/search")
    public String index(String type, String name,String fy,String atype,Integer currentPage, Model model) throws UnsupportedEncodingException {
        if(name == null||name.equals("")||name.equals("null")){
            name = "";
        }
        if(fy == null||fy.equals("")||fy.equals("null")){
            fy = "";
        }
        if(atype == null||atype.equals("")||atype.equals("null")){
            atype = "";
        }

        if(currentPage == null){
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        String encodedName = "";
        List<ArtType> artTypes = new ArrayList<>();
        switch (type){
            // 艺术家
            case "1":
                encodedName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
                return "redirect:/artist/list?name="+encodedName;
            // 艺术品
            case "2":
                menuList.get(3).setIsActice(1);
                model.addAttribute("menus",menuList);
                Artwork artwork = new Artwork();
                artwork.setArtwoktType(atype);
                Integer artWorkSize = 16;
                IPage<Artwork> artworkIPage =  artWorkService.getPage(currentPage,artWorkSize,artwork);
                // 首页显示艺术类型
                artTypes = artTypeService.getList();
                model.addAttribute("artTypes",artTypes);
                // 当前页
                model.addAttribute("currentPage",artworkIPage.getCurrent());
                System.out.println(artworkIPage.getPages()+"总页数");
                // 总页数
                model.addAttribute("totalPages",artworkIPage.getPages());
                // 分页页码
                model.addAttribute("pages",getPageList((int) artworkIPage.getPages(),currentPage,5));
                // 分页数据
                model.addAttribute("artworks",artworkIPage.getRecords());
                model.addAttribute("keyword",name);
                model.addAttribute("atype",atype);
                encodedName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
                return "redirect:/artwork/list?name="+encodedName;
            // 机构
            case "3":
                Depart depart = new Depart();
                // 名称搜索
                if (name == null) {
                    name = "";
                }else{
                    depart.setName(name);
                }
                System.out.println(depart);


                List<Region> regions = departService.getDepartRegionList(10);
                model.addAttribute("regions",regions);

                List<DepartType> departTypes = departTypeService.getDepartTypeList(10);
                model.addAttribute("departTypes",departTypes);

                // 推荐新闻 2条
                List<Article> recommendArticles = articleService.getRecommendArticles(2);
                model.addAttribute("recommendArticles",recommendArticles);
                // 首页新闻 7 条
                List<Article> indexArticles = articleService.getIndexArticles(7);
                model.addAttribute("indexArticles",indexArticles);
                // 推荐艺术家 1 条
                List<Artist> recommendArtists = artistService.getRecommendArtists(1);
                model.addAttribute("recommendArtists",recommendArtists);
                // 名家简历 7条
                List<Artist> famousArtists = artistService.getFamousArtists(7);
                model.addAttribute("famousArtists",famousArtists);
                // 首页显示艺术家
                List<Artist> indexArtists = artistService.getIndexArtists(32);
                model.addAttribute("indexArtists",indexArtists);

                // 首页显示机构 4条

                List<Depart> indexDeparts = departService.getDepartList(depart);
                System.out.println(indexDeparts.size());
                model.addAttribute("indexDeparts",indexDeparts);

                return "pc/depart/depart_list";

            // 文章
            case "4":
                Article article = new Article();
                article.setName(name);
                List<Article> articles = articleService.getArticleByName(article);
                System.out.println("搜索文章");
                System.out.println(articles.size());
                model.addAttribute("articles",articles);
                return "pc/search_article_list";
            //

        }



        return "pc/search_article_list";
    }
}
